﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Wormhole.Engine.Communication.Messages
{
	/// <author>Hannes Foulds</author>
	/// <summary>
	/// This class represents game list messages.
	/// </summary>
	public class GameListMessage : Message
	{
		#region Constructor
		/// <summary>
		/// Create a default instance of the class.
		/// </summary>
		public GameListMessage()
		{
		}
		
		/// <summary>
		/// Initialize the class with the values provided.
		/// </summary>
		/// <param name="message">The string message to parse.</param>
		public GameListMessage(string message, Actions action, string requestor)
		{
			this.Action = action;
			this.Parse(message, requestor);
		}
		#endregion

		#region To String
		/// <summary>
		/// Convert the message to a string that can be send via the communications channel.
		/// </summary>
		/// <returns>Returns the string representation of the message.</returns>
		public override string ToString()
		{
			if (this.Action == Actions.GameListRequest)
				return EnumConvertor<Actions>.ToString(this.Action);
			else if (this.Action == Actions.GameList)
				return string.Format("{0}|{1}|{2}", EnumConvertor<Actions>.ToString(this.Action), this.Game.ID, this.EncodeString(this.Game.Description));
			return null;
		}
		#endregion

		#region Parse
		/// <summary>
		/// Parse a received game list message.
		/// </summary>
		/// <param name="message">The string message to parse.</param>
		protected void Parse(string message, string requestor)
		{
			if (this.Action == Actions.GameList)
			{
				this.Game = new Game();
				string[] messageParts = message.Split('|');

				this.Game.ID = new Guid(messageParts[1]);
				this.Game.Description = this.DecodeString(messageParts[2]);
				this.Game.OpponentName = requestor;
			}
		}
		#endregion
	}
}
